

#include "temphumiditymodule_thread.h"
#include "bsp_temphumiditymodule_handler.h"
#include "ec_bsp_aht21_driver.h"
#include "drv_iic.h"
#include "cmsis_os.h"
#include "main.h"
#include "elog.h"
#include "hal_adc.h"
#include "stdlib.h"
#include "adtask.h"
QueueHandle_t xQueue;
//QueueHandle_t Send_to_taskxQueue;
void temp_humi_callback(float *temperature, float *humidity)
{
	uartprintf("task2 :temperature = %f, humidity = %f\n", *temperature, *humidity);
}
SemaphoreHandle_t xMutex;
void task2(void *data)
{
	temp_humi_event_t temp_humi_event11;
	temp_humi_event11.callback = temp_humi_callback;
	temp_humi_event11.lifetime = 2000;
	temp_humi_event11.type_of_data = TEMP_HUMI_EVENT_TYPE_BOTH;
	while(1)
	{
	
		vTaskDelay(500);
		mhumiditymodule_event_read(&temp_humi_event11);
	}
}
void temp_humi_callback1(float *temperature, float *humidity)
{
	uartprintf("task3:temperature = %f, humidity = %f\n", *temperature, *humidity);
}
void task3(void *data)
{
	temp_humi_event_t temp_humi_event111;
	temp_humi_event111.callback = temp_humi_callback1;
	temp_humi_event111.lifetime = 2000;
	temp_humi_event111.type_of_data = TEMP_HUMI_EVENT_TYPE_TEMP;
	while(1)
	{

    log_i("Hello EasyLogger!");

		vTaskDelay(500);
		mhumiditymodule_event_read(&temp_humi_event111);
	}
}

QueueHandle_t xQueue1;
BaseType_t xHigherPriorityTaskWoken1 = pdFALSE;

//void gather_dc(void *para)
//{
//	
//	uint32_t *buffer2 = malloc(sizeof(uint32_t));
//	uint32_t *buffer1 = malloc(sizeof(uint32_t));
//	HAL_ADC_Start_DMA(&hadc1,buffer1,sizeof(uint32_t));
//	Send_to_taskxQueue = xQueueGenericCreate( 1, sizeof(uint32_t),queueQUEUE_TYPE_BASE);
//	xQueue1 = xQueueGenericCreate( 1, sizeof(uint32_t),queueQUEUE_TYPE_BASE);
//	
//	static uint8_t flag = 0;
//	if(xQueue1 == NULL)
//	{
//		uartprintf("no xQueue1\r\n!");
//	}
//	uartprintf("ok xQueue1\r\n!");
//	while(1)
//	{
//	    if(flag == 0)
//		{
//		  HAL_ADC_Stop_DMA(&hadc1);	
//		  HAL_ADC_Start_DMA(&hadc1,buffer2,sizeof(uint32_t));			
//          xQueueOverwriteFromISR(xQueue1,buffer1,&xHigherPriorityTaskWoken1);
//		  
//			
//		  flag =1;
//		}else{

//		  HAL_ADC_Stop_DMA(&hadc1);
//		  HAL_ADC_Start_DMA(&hadc1,buffer1,sizeof(uint32_t));			
//		  xQueueOverwriteFromISR(xQueue1,buffer2,&xHigherPriorityTaskWoken1);
//		  
//		  flag =0;
//		}
//	}
//	
//}


//void readdc(void *para)
//{
//	uint32_t *dc;
//	uint32_t da;
//	while(1)
//	{
//		vTaskDelay(100);		
//		xQueuePeek(xQueue1 ,&da,0);		
//		uartprintf("adc_dc====:%.3f V\r\n",da*(3.3/4096.0));
//	}
//	
//	
//}

void queuecreate(uint32_t eLength,uint32_t item,void **xQueue_handler)
{
	*xQueue_handler = xQueueGenericCreate( eLength, item,queueQUEUE_TYPE_BASE);
}
int8_t tempxQueueGenericSend(void *item,uint32_t timeout,void *xQueue_handler)
{
	xQueueGenericSend(xQueue_handler,item,timeout, queueSEND_TO_BACK);
	return 0;
}

void tempxQueueGenericread(void *item,uint32_t timeout,void *xQueue_handler)
{
	xQueueReceive( xQueue_handler, item, timeout);
}
uint32_t nowtime()
{
	uint64_t count = xTaskGetTickCount();
//	if( count > 0xffffff00)
//	{
//		count -=0xffffff00;
//	}
	return (uint32_t)count;
}

void mutualexcluscreate(void)
{
	xMutex = xSemaphoreCreateMutex();
}

void xsemaphorege(uint32_t timeout)
{
	xSemaphoreTake(xMutex, timeout);
}

void xsemaphorefree(void)
{
	xSemaphoreGive(xMutex);
}
void create_taskinit(void)
{
   
	static ec_bsp_thermhumiditymodule_handler_public_t taskhandler;

	taskhandler.iic_instance.pfinit = iicinit;
	taskhandler.iic_instance.pfdeinit = iicdeinit;
	taskhandler.iic_instance.pfread_reg = read_reg;
	taskhandler.iic_instance.pfwrite_reg = write_reg;
	taskhandler.timebase_interface.delay_ms = vTaskDelay;
	taskhandler.timebase_interface.delay_us = vTaskDelay;
	taskhandler.rtos_yeild.getnowtime   = nowtime;
	taskhandler.rtos_yeild.os_tick      = vTaskDelay;
	taskhandler.rtos_yeild.queue_create = queuecreate;
	taskhandler.rtos_yeild.queue_send 	= tempxQueueGenericSend;
	taskhandler.rtos_yeild.queue_read   = tempxQueueGenericread;
	taskhandler.rtos_yeild.semaphorehandle_create = mutualexcluscreate;
	taskhandler.rtos_yeild.xsemaphoreget = xsemaphorege;
	taskhandler.rtos_yeild.xsemaphorefree = xsemaphorefree;
	
	
//	xTaskCreate(thermhumiditymodule_thread , "thread" , 1096 ,&taskhandler,2, NULL);
//	xTaskCreate(task2 ,"Task2" ,256 , NULL , 2 , NULL);
	//xTaskCreate(task3 ,"Task3" ,256 , NULL , 2 , NULL);
//	xTaskCreate(gather_dc ,"gather_dc" ,512 , NULL , 2 , NULL);
//	xTaskCreate(readdc,"readdc" ,256 , NULL , 2 , NULL);
	xTaskCreate(adc_thread ,"adc_thread" ,512 , NULL , 2 , NULL);
	xTaskCreate(adc_information_dispose,"adc_information_dispose" ,256 , NULL , 2 , NULL);
}

